﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Interfaces.Models;
using LinqToYql.Interfaces;
using YQLDataProvider.Mappings;
using HelperTools;

namespace YQLDataProvider.YqlDataLoaders
{
    public class XmlXchangeDataLoader : IDataLoader
    {
        public IEnumerable<object> LoadData(object source)
        {
            var document = source as XDocument;
            if(document == null)
                return new LinkedList<IXchangeRate>();
            XDocumentToYqlDataMappings.CreateXChangeRateMap();
            var xchangeCollection = document.Root.With(x => x.Element("results")).With(y => y.Elements("rate"));
            var rates = AutoMapper.Mapper.Map<IEnumerable<XElement>, IEnumerable<IXchangeRate>>(xchangeCollection).ToList();
            foreach (var xchangeRate in rates)
                xchangeRate.Pair.Rateses.Add(xchangeRate);
            return rates;
        }

       
    }
}
